import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

public class test30 {
    public static void main(String[] args) throws IOException {
        show();
    }

    private static void show() throws IOException {
        Scanner sc=new Scanner(System.in);
        while (true){
            System.out.println("1.账号登陆");
            System.out.println("2.游客浏览");
            System.out.println("3.请输入序号");
            int num=sc.nextInt();
            if (num==1){
                login();
            }else if (num==2){
                otherLogin();
            }else {
                System.out.println("序号错误无法执行命令");
            }
        }
    }

    public static boolean login() throws IOException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String user=sc.nextLine();
        System.out.println("请输入密码");
        String pasd=sc.nextLine();
        Properties pro=new Properties();
        pro.load(new FileReader("user.properties"));
        String flag=null;
        for (Object o : pro.keySet()) {
            String s=String.valueOf(o);
            String s1=String.valueOf(pro.get(o));
            if (s.equals(user)&&s1.equals(pasd)){
                flag=s;
            }
        }
        boolean a;
        if (flag!=null){
            Date d=new Date();
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String format = df.format(d);
            String s=flag+" "+format+" 登陆成功！";
            System.out.println(" 登陆成功！");
            a=true;
            FileUtils.writeStringToFile(new File("data3.txt"),s);
            System.exit(0);
        }else {
            Date d=new Date();
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String format = df.format(d);
            String s=flag+" "+format+" 登陆失败！";
            System.out.println(" 登陆失败！");
            a=false;
            FileUtils.writeStringToFile(new File("D:\\zuoye\\GuiWeiJun\\day04_Test\\data.txt"),s);

        }
        return a;
    }
    public static void otherLogin() throws IOException {
        Date d=new Date();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = df.format(d);
        String s="游客"+" "+format+"进行浏览";
        FileUtils.writeStringToFile(new File("D:\\zuoye\\GuiWeiJun\\day04_Test\\data.txt"),s);
        System.exit(0);
    }
}
